// pages/clothDetail/clothDetail.js
var clothUtil = require("../../utils/clothUtil.js");
var bagUtil = require("../../utils/bagUtil.js");
var wxParser = require('../../wxParser/index');

var productId = 0;
var productName = "";
Page({
  data:{
    loadHide: false,
    swiperCur: 0,
    showRelate: false, // 是否显示相关推荐
    productInfo: {}, //商品信息
    fabricInfo: {}, // 商品对应的面料信息
    vendorInfo: {}, // 供应商信息
    isTitleShow: true,
    favoriteId: 0, // 收藏记录ID，便于删除
    isOpShow: true,// 是否显示底部
    isShowDetail: false, // 是否显示详情
    isShowBag: false, // 是否显示购物袋
    isOpDetailClick: false, // 是否实现
    bagNum: 0,
    clothSKU: [], // 成衣SKU
    fabricSKU: [], // 对应面料SKU
    SKUNum: {} // 每个SKUID选择的数量
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    console.log(options);
    productId = options.productId || 865;
    productName = options.productName;

    // if(typeof productId == 'undefined') return;
    if(productName){
      wx.setNavigationBarTitle({
        title: productName
      });
    }
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          windowHeight:res.windowHeight
        });
      }
    });

    clothUtil.getProductInfo(productId, function(data){
      console.log(data);
        // 增加相关滑动
        data.response.images[data.response.images.length] = new Array();
        that.setData({
          productInfo: data,
          loadHide: true
        });
        // 获取成衣的面料信息
        // clothUtil.getProductInfo(data.response.parent_id, function(data){
        //   that.setData({
        //     fabricInfo: data
        //   });
        // });
        // 获取content信息
        if(data.response.content_id){
          wx.BaaS.getContent({ richTextID: data.response.content_id}).then( (res) => {
            wxParser.parse({
              bind: 'richText',
              html: res.data.content,
              target: that,
              enablePreviewImage: false
            });
          }, (err) => {
              // err
          });
        }
        // 获取相关推荐
        if(data.response.parent_id){
          clothUtil.getClothRelate(productId, data.response.parent_id, getApp().globalData.relateNum, function(data){    
            that.data.productInfo.response.images[that.data.productInfo.response.images.length-1] = data;
            that.setData({
              productInfo: that.data.productInfo
            });
          });
        }
        
        // 获取本身的sku 和 parent_id对应的sku
        clothUtil.getProductSKU(productId, function(data){
          that.setData({clothSKU: data});
        });
        if(data.response.parent_id){
          clothUtil.getProductSKU(data.response.parent_id, function(data){
            that.setData({fabricSKU: data});
          });
        }

        if(data.response.vendor_id){
          clothUtil.getVendorInfo(data.response.vendor_id, function(data){
            console.log(data);
            that.setData({vendorInfo: data});
          });
        }
    });

    clothUtil.checkFavorite(productId, function(data){
        // 设置收藏的ID
        that.setData({favoriteId:data});
    });
    bagUtil.infoBag(function(num){
        that.setData({bagNum: num});
    });
    
  },
  swiperChange: function(e){
    if(e.detail.current == this.data.productInfo.response.images.length - 1){
      this.setData({showRelate: true});
    }else{
      this.setData({showRelate: false});
    }
    this.setData({
      swiperCur: e.detail.current
    });
  },
  onTapDetail: function(){
    var showDetail = this.data.isShowDetail;
    this.setData({
      isShowDetail: !showDetail,
      isTitleShow: showDetail
    });
  },
  onTapFavorite: function(){
    var opType = this.data.favoriteId != 0 ? false : true;
    var that = this;
    console.log(productId);
    clothUtil.favorite(productId, opType, this.data.favoriteId, function(data){
        that.setData({favoriteId:data});
    });
  },
  onTapBag: function(){
    // 点击购物袋按钮
    if(this.data.isShowBag){
      // 加入购物袋
      var that = this;
      var nums = 0;
      for(var i in this.data.SKUNum){
        nums += this.data.SKUNum[i];
      }
      if(nums < 1) return;
      bagUtil.addBag(this.data.SKUNum, function(num){
        that.setData({bagNum: that.data.bagNum + num, SKUNum:{}});
      });
      this.setData({
        isShowDetail: false,
        isTitleShow: true,
        isShowBag: false
      });
    }else{
      this.setData({
        isShowDetail: false,
        isTitleShow: false,
        isShowBag: true
      });
    }
  },
  closeBag: function(){
    this.setData({
        isShowDetail: false,
        isTitleShow: true,
        isShowBag: false
    });
  },
  clothDown: function(e){
    var skuid = e.currentTarget.dataset.skuid;
    if(typeof this.data.SKUNum[skuid] == 'undefined' || parseInt(this.data.SKUNum[skuid]) < 1) return;
    this.data.SKUNum[skuid] = this.data.SKUNum[skuid] - 1;
    if(this.data.SKUNum[skuid] == 0) delete this.data.SKUNum[skuid];
    this.setData({
      SKUNum: this.data.SKUNum
    });
  },
  clothUp: function(e){
    var skuid = e.currentTarget.dataset.skuid;
    var skumax = e.currentTarget.dataset.skumax;
    typeof this.data.SKUNum[skuid] == 'undefined' ? this.data.SKUNum[skuid] = 0 : this.data.SKUNum[skuid];
    if(skumax && parseInt(this.data.SKUNum[skuid]) >= skumax) return;
    typeof this.data.SKUNum[skuid] == "undefined" ? this.data.SKUNum[skuid] = 1 : this.data.SKUNum[skuid]++;
    this.setData({
      SKUNum: this.data.SKUNum
    });
  },

  gotoBag: function(){
    wx.switchTab({
      url: '/pages/bag/bag'
    })
  },
  gotoDetail: function(e){
      var productId = e.currentTarget.dataset.productid;
      var productName = e.currentTarget.dataset.productname;
      
      var url = '/pages/clothDetail/clothDetail?productId='+productId+'&productName='+productName;
      wx.redirectTo({
          url: url
      })
  },
  onShareAppMessage: function () {
    return {
      title: this.data.productInfo.response.title,
      path: '/pages/clothDetail/clothDetail?productId='+this.data.productInfo.id+'&productName='+this.data.productInfo.response.title
    }
  }
})